<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\balancediscount\model;

class OrderShare extends \app\BaseModel
{
    protected $name = "massage_balance_discount_order_share";
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function orderShareAdd($order_id, $uniacid, $list, $admin_id, $p_order_id = 0, $type = 1)
    {
        if(empty($list)) {
            return true;
        }
        $p_order_id = $type == 1 ? $order_id : $p_order_id;
        if($type == 1) {
            if(!empty($admin_id)) {
                $admin_model = new \app\massage\model\Admin();
                $admin = $admin_model->where(["id" => $admin_id, "status" => 1, "agent_coach_auth" => 1])->count();
                if($admin == 0) {
                    $admin_id = 0;
                }
            }
            $arr = getConfigSettingArr($uniacid, ["balance_discount_coach_balance", "balance_discount_admin_balance"]);
            $balance_discount_coach_balance = $arr["balance_discount_coach_balance"];
            $balance_discount_admin_balance = !empty($admin_id) ? $arr["balance_discount_admin_balance"] : 0;
        } else {
            $data = $this->dataInfo(["p_order_id" => $p_order_id, "status" => 2]);
            $balance_discount_coach_balance = $data["coach_balance"];
            $balance_discount_admin_balance = $data["admin_balance"];
        }
        foreach ($list as $key => $value) {
            $insert = ["uniacid" => $uniacid, "order_id" => $order_id, "p_order_id" => $p_order_id, "card_id" => $value["id"], "coach_balance" => $balance_discount_coach_balance, "admin_balance" => $balance_discount_admin_balance, "company_balance" => 100 - $balance_discount_coach_balance - $balance_discount_admin_balance, "status" => 1, "type" => $type, "cash" => $value["total_cash"], "discount" => $value["discount"], "service_cash" => $value["true_total_cash"], "value_cash" => $value["value_cash"], "car_cash" => $value["discount_car_cash"], "discount_cash" => $value["discount_cash"], "coach_share_cash" => $balance_discount_coach_balance / 100 * $value["discount_cash"], "admin_share_cash" => $balance_discount_admin_balance / 100 * $value["discount_cash"], "company_share_cash" => (100 - $balance_discount_coach_balance - $balance_discount_admin_balance) / 100 * $value["discount_cash"]];
            $this->dataAdd($insert);
        }
        return true;
    }
    public function orderShareData($order_id, $type = 1)
    {
        $dis[] = ["a.p_order_id", "=", $order_id];
        $dis[] = ["a.status", "in", [-2, 2]];
        $data = $this->alias("a")->join("massage_balance_discount_user_card b", "a.card_id = b.id")->where($dis)->field("b.title,b.discount,sum(a.value_cash) as value_cash,sum(a.cash) as cash,sum(a.discount_cash) as discount_cash")->group("a.card_id")->order("b.discount,a.id desc")->select()->toArray();
        $arr["balance_discount_list"] = $data;
        $arr["balance_discount_num"] = count($data);
        $arr["balance_discount_cash"] = round(array_sum(array_column($data, "discount_cash")), 2);
        return $arr;
    }
    public function refundUpdateOrderBalanceDiscount($order_id, $refund_id, $user_id, $service_cash, $car_cash)
    {
        if($service_cash == 0 && $car_cash == 0) {
            return true;
        }
        $dis = ["p_order_id" => $order_id, "status" => 2];
        $list = $this->where($dis)->select()->toArray();
        if(empty($list)) {
            return false;
        }
        $water_model = new CardWater();
        \think\facade\Db::startTrans();
        foreach ($list as $value) {
            $refund_cash = 0;
            $i = 0;
            if(0 < $value["car_cash"] && 0 < $car_cash) {
                $i = 1;
                $true_car_cash = $value["car_cash"] < $car_cash ? $value["car_cash"] : $car_cash;
                $car_cash -= $true_car_cash;
                $value["car_cash"] -= $true_car_cash;
                $value["cash"] -= $true_car_cash;
                $value["value_cash"] -= $true_car_cash;
                $refund_cash += $true_car_cash;
            }
            if(0 < $value["service_cash"] && 0 < $service_cash) {
                $i = 1;
                $true_service_cash = $value["value_cash"] < $service_cash ? $value["value_cash"] : $service_cash;
                $service_cash -= $true_service_cash;
                $value["service_cash"] = round($value["service_cash"] - $true_service_cash / $value["discount"] * 10, 2);
                $value["cash"] = round($value["cash"] - $true_service_cash / $value["discount"] * 10, 2);
                $value["value_cash"] = round($value["value_cash"] - $true_service_cash, 2);
                $value["discount_cash"] = $value["service_cash"] - $value["value_cash"];
                $value["coach_share_cash"] = $value["discount_cash"] * $value["coach_balance"] / 100;
                $value["admin_share_cash"] = $value["discount_cash"] * $value["admin_balance"] / 100;
                $value["company_share_cash"] = $value["discount_cash"] * $value["company_balance"] / 100;
                $refund_cash += $true_service_cash;
            }
            if($i == 1) {
                if($value["value_cash"] <= 0) {
                    $value["status"] = -2;
                }
                $res = $this->where(["id" => $value["id"]])->update($value);
                if($res == 0) {
                    \think\facade\Db::rollback();
                    return false;
                }
                $res = $water_model->updateCash($value["uniacid"], $value["card_id"], $refund_cash, 1, $order_id, 3, $user_id, $refund_id);
                if($res == 0) {
                    \think\facade\Db::rollback();
                    return false;
                }
            }
        }
        \think\facade\Db::commit();
        return true;
    }
}

?>